package com.xinggq.pay.service.pay.channel.yse.pay.qrpay;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.xinggq.pay.api.dto.YseCommonPayReqDTO;
import com.xinggq.pay.api.exception.PayException;
import com.xinggq.pay.service.pay.channel.yse.enums.YseMethodEnum;
import com.xinggq.pay.service.pay.channel.PayChannelPayApiService;
import com.xinggq.pay.service.adaptor.YseApiAdaptor;
import com.xinggq.pay.service.entity.YsePayConfigEntity;
import com.xinggq.pay.service.pay.channel.yse.enums.YsePayWayBindEnum;
import com.xinggq.pay.service.pay.channel.yse.pay.common.YsePayCommonDTO;
import com.xinggq.pay.service.pay.channel.yse.pay.qrpay.dto.YseQrPayRespDTO;
import com.xinggq.pay.service.service.YsePayConfigService;
import com.xinggq.pay.service.pay.channel.yse.utils.YseUtil;
import com.xinggq.pay.service.pay.channel.yse.pay.qrpay.dto.YseQrPayReqDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 银盛扫码支付实现类
 * @Description
 * @Author xinggq
 * @Date 2025/9/12
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class YseQrPayServiceImpl implements
    PayChannelPayApiService<YseCommonPayReqDTO,YseQrPayRespDTO> {

  private final YseApiAdaptor yseApiAdaptor;
  private final YsePayConfigService ysePayConfigService;

  @Override
  public YseQrPayRespDTO pay(YseCommonPayReqDTO payParamDto) {
    Validator.validateNotNull(payParamDto, "银盛接口参数不能为空");
    // 获取数据库中的银盛当前支付渠道配置
    YsePayConfigEntity ysePayConfig = ysePayConfigService.getConfigByMethod(YseMethodEnum.QR_PAY);
    Validator.validateNotNull(ysePayConfig,
        String.format("银盛接口配置为空，支付方式:{%s}", YseMethodEnum.QR_PAY.getMethod()));
    if (!isQrPayWay(payParamDto)) {
      throw new PayException("不支持的银盛接口调用");
    }
    // 创建银盛参数
    String requestData = createYsePayStr(payParamDto, ysePayConfig);
    String respStr = yseApiAdaptor.pay(payParamDto.getTradeNo(),payParamDto.getChannelOutTradeNo(), requestData,
        YseMethodEnum.QR_PAY.getGatewayUrl());
    return JSONObject.parseObject(respStr, YseQrPayRespDTO.class);
  }

  private String createYsePayStr(YseCommonPayReqDTO payParamDto, YsePayConfigEntity ysePayConfig) {
    if (payParamDto.getYsePayWay() == YsePayWayBindEnum.YSE_ALIPAY_PAY.getPayWayEnum()) {
      return buildAliPayDto(payParamDto, ysePayConfig);
    } else if (payParamDto.getYsePayWay() == YsePayWayBindEnum.YSE_QQ_QR.getPayWayEnum()) {
      return buildQqPayDto(payParamDto, ysePayConfig);
    } else if (payParamDto.getYsePayWay() == YsePayWayBindEnum.YSE_UNION_PAY.getPayWayEnum()) {
      return buildUnionPayDto(payParamDto, ysePayConfig);
    } else {
      return buildCmbPayDto(payParamDto, ysePayConfig);
    }
  }

  /**
   *  构建招商支付参数
   */
  private String buildCmbPayDto(YseCommonPayReqDTO payParamDto, YsePayConfigEntity ysePayConfig) {
    // 创建银盛二维码支付参数
    YseQrPayReqDTO qrPayReqDTO = YseQrPayReqDTO.buildYseQrPayReqDTO();
    qrPayReqDTO.setBankType(YsePayWayBindEnum.YSE_CMB.getYseCode());
    return buildQrcodePayCommonDot(qrPayReqDTO, payParamDto, ysePayConfig);
  }


  /**
   * 构建银联支付参数
   */
  private String buildUnionPayDto(YseCommonPayReqDTO payParamDto, YsePayConfigEntity ysePayConfig) {
    // 创建银盛二维码支付参数
    YseQrPayReqDTO qrPayReqDTO = YseQrPayReqDTO.buildYseQrPayReqDTO();
    qrPayReqDTO.setBankType(YsePayWayBindEnum.YSE_UNION_PAY.getYseCode());
    return buildQrcodePayCommonDot(qrPayReqDTO, payParamDto, ysePayConfig);
  }


  /**
   * 构建QQ二维码支付参数
   * @return
   */
  private String buildQqPayDto(YseCommonPayReqDTO payParamDto, YsePayConfigEntity ysePayConfig) {
    // 创建银盛二维码支付参数
    YseQrPayReqDTO qrPayReqDTO = YseQrPayReqDTO.buildYseQrPayReqDTO();
    qrPayReqDTO.setBankType(YsePayWayBindEnum.YSE_QQ_QR.getYseCode());
    return buildQrcodePayCommonDot(qrPayReqDTO, payParamDto, ysePayConfig);
  }


  /**
   * 构建支付宝二维码支付参数
   * @return
   */
  private String buildAliPayDto(YseCommonPayReqDTO payParamDto, YsePayConfigEntity ysePayConfig) {
    // 创建银盛二维码支付参数
    YseQrPayReqDTO qrPayReqDTO = YseQrPayReqDTO.buildYseQrPayReqDTO();
    qrPayReqDTO.setBankType(YsePayWayBindEnum.YSE_ALIPAY_PAY.getYseCode());
    return buildQrcodePayCommonDot(qrPayReqDTO, payParamDto, ysePayConfig);
  }

  /**
   * 构建扫码支付公共参数
   */
  private String buildQrcodePayCommonDot(
      YseQrPayReqDTO qrPayReqDTO, YseCommonPayReqDTO payParamDto,
      YsePayConfigEntity ysePayConfig) {
    qrPayReqDTO.setSellerId(ysePayConfig.getSellerId());
    qrPayReqDTO.setBusinessCode(ysePayConfig.getBusinessCode());
    qrPayReqDTO.setOutTradeNo(payParamDto.getChannelOutTradeNo());
    qrPayReqDTO.setSubject(payParamDto.getSubject());
    qrPayReqDTO.setTotalAmount(payParamDto.getTotalAmount());
    return buildWithPayCommonDto(ysePayConfig,
        JSONUtil.toJsonStr(BeanUtil.beanToMap(qrPayReqDTO, true, true)));
  }


  /**
   * 构建通用参数
   */
  private String buildWithPayCommonDto(YsePayConfigEntity ysePayConfig, String bizContent) {
    YsePayCommonDTO ysePayCommonDTO = YsePayCommonDTO.buildYseCommonPayDto(ysePayConfig, bizContent,YseMethodEnum.QR_PAY);
    return YseUtil.mapToSortedString(BeanUtil.beanToMap(ysePayCommonDTO, true, true));
  }


  private boolean isQrPayWay(YseCommonPayReqDTO payParamDto) {
    if (payParamDto.getYsePayWay() == null) {
      // 默认支付宝扫码支付
      payParamDto.setYsePayWay(YsePayWayBindEnum.YSE_ALIPAY_PAY.getPayWayEnum());
      return true;
    }
    return payParamDto.getYsePayWay() == YsePayWayBindEnum.YSE_ALIPAY_PAY.getPayWayEnum()
        || payParamDto.getYsePayWay() == YsePayWayBindEnum.YSE_QQ_QR.getPayWayEnum()
        || payParamDto.getYsePayWay() == YsePayWayBindEnum.YSE_UNION_PAY.getPayWayEnum()
        || payParamDto.getYsePayWay() == YsePayWayBindEnum.YSE_CMB.getPayWayEnum();
  }
}
